package struct.scene.items
{
	import collection.DataCollection;
	
	import com.common.displayObjects.Component;
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	
	import data.PlayerVO;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	import struct.scene.SceneModule;
	
	import utils.Util;
	
	/**
	 * 角色， 包括AI 
	 * @author Administrator
	 * 
	 */	
	public class Player extends Component
	{
		public var playerVO:PlayerVO;
		public var ui:*;
		public var isUser:Boolean = false; //true：这个角色是用户正在控制的角色     不是AI      false： AI
		public var position:Point; //当前this的位置
		public var xIndex:int;
		public var yIndex:int;
		public var r:int = 15; //距离检测半径
		
		public function Player(playerVO:PlayerVO, isUser:Boolean)
		{
			this.playerVO = playerVO;
			this.isUser = isUser;
			super();
			if (isUser) //用户角色可控制
				addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		protected function onAdded(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		public var testBorder:Shape; //检测边界
		override protected function createChildren():void
		{
			super.createChildren();
			
			var c:Class = Util.getClass(playerVO.bitmapClassname);
			ui = new c();
			if (ui is BitmapData)
			{
				var bm:Bitmap = new Bitmap(ui);
				bm.x = - bm.width * .5;
				bm.y = - bm.height + 10;
				addChild(bm);
			}else
			{
				ui.x = - (ui as DisplayObject).width * .5;
				ui.y = - (ui as DisplayObject).height + 10;
				addChild(ui);
			}
			
			testBorder = new Shape();
			var g:Graphics = testBorder.graphics;
			g.beginFill(0xff0000, .3);
			g.drawCircle(0, 0, r);
			g.endFill();
			addChild(testBorder);
		}
		
		private var gridMovingTo:Grid; // 朝向这个grid移动
		public function move(grid:Grid):void
		{
			if (grid == gridMovingTo)return;
			isMoving = true;
			gridMovingTo = grid;
			var distance:Number = Point.distance(new Point(this.x, this.y), new Point(grid.x + grid.width * .5, grid.y + grid.height * .5));
			if (isUser)
			{
				var time:Number = (distance / DataCollection.userMoveLen) * DataCollection.TWEENTIME;
			}else
			{
				time = (distance / DataCollection.aiMoveLen) * DataCollection.TWEENTIME;
			}
			TweenLite.to(this, time, {x:grid.x + grid.width * .5, y:grid.y + grid.height * .5, ease:Linear.easeNone, onComplete:onPlayerReach});
		}
		
		protected function onPlayerReach():void
		{
			isMoving = false;
		}
		
		public function stop():void
		{
			gridMovingTo = null;
			isMoving = false;
			TweenLite.killTweensOf(this);
		}
		
		public var boUpDown:Boolean;
		public var boRightDown:Boolean;
		public var boDownDown:Boolean;
		public var boLeftDown:Boolean;
		public var isMoving:Boolean; //是否正在移动中
		
		protected function onKeyDown(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.UP: //1
					boUpDown = true;
					break;
				case Keyboard.RIGHT://2
					boRightDown = true;
					break;
				case Keyboard.DOWN://4
					boDownDown = true;
					break;
				case Keyboard.LEFT://8
					boLeftDown = true;
					break;
			}
		}
		
		protected function onKeyUp(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.UP: //1
					boUpDown = false;
					break;
				case Keyboard.RIGHT://2
					boRightDown = false;
					break;
				case Keyboard.DOWN://4
					boDownDown = false;
					break;
				case Keyboard.LEFT://8
					boLeftDown = false;
					break;
			}
		}
		
		override public function dispose():void
		{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			super.dispose();
			stop();
		}
		
		public function updatePosition():void
		{
			xIndex = (x / SceneModule.getInstance().mapVO.gridWidth) >>0;
			yIndex = (y / SceneModule.getInstance().mapVO.gridHeight) >>0;
		}
	}
}